package com.ac.scum.log.task.task;


import com.ac.scum.log.task.mapper.log.PlayerScoreKillMapper;
import com.ac.scum.player.score.bean.PlayerScoreKill;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.List;
import java.util.Set;

@Slf4j
@Service
@RequiredArgsConstructor
public class KillScoreTask {

    private final RedisTemplate<String, Object> redisTemplate;
    private final PlayerScoreKillMapper playerScoreKillMapper;

    @Scheduled(cron = "0 */2 * * * ?")
    public void killsScore() {
        Set<String> keys = redisTemplate.keys("kills-score:*");
        if (keys == null || keys.isEmpty()) {
            return;
        }
        for (String key : keys) {
            List<Object> range = redisTemplate.opsForList().range(key, 0, -1);
            if (range == null || range.isEmpty()) {
                continue;
            }
            //获取到后删除
            try {
                redisTemplate.delete(key);
                long killsCount = range.stream().map(item -> (Integer) item).count();
                String[] split = key.replace("kills-score:", "").split("-");
                Long serverId = Long.parseLong(split[0]);
                Long playerId = Long.parseLong(split[1]);
                Long steamId = Long.parseLong(split[2]);
                PlayerScoreKill playerScoreKill = playerScoreKillMapper.selectOne(Wrappers.lambdaQuery(PlayerScoreKill.class)
                        .eq(PlayerScoreKill::getServerId, serverId)
                        .eq(PlayerScoreKill::getPlayerId, playerId)
                        .eq(PlayerScoreKill::getCountDate, LocalDate.now()));
                if (playerScoreKill == null) {
                    playerScoreKill = new PlayerScoreKill();
                    playerScoreKill.setCountDate(LocalDate.now());
                    playerScoreKill.setSteamId(steamId);
                    playerScoreKill.setServerId(serverId);
                    playerScoreKill.setPlayerId(playerId);
                    playerScoreKill.setKills(0);
                    playerScoreKill.setDies(0);
                }

                playerScoreKill.setKills(playerScoreKill.getKills() + (int) killsCount);

                if (playerScoreKill.getId() == null) {
                    playerScoreKillMapper.insert(playerScoreKill);
                } else {
                    playerScoreKillMapper.updateById(playerScoreKill);
                }
            } catch (Exception e) {
                log.error("击杀数据统计失败 {}", e.getMessage());
            }
        }
        log.info("击杀数据统计完成");
    }

    @Scheduled(cron = "0 */2 * * * ?")
    public void diesScore() {
        Set<String> keys = redisTemplate.keys("dies-score:*");
        if (keys == null || keys.isEmpty()) {
            return;
        }
        for (String key : keys) {
            List<Object> range = redisTemplate.opsForList().range(key, 0, -1);
            if (range == null || range.isEmpty()) {
                continue;
            }
            //获取到后删除
            redisTemplate.delete(key);
            long diesCount = range.stream().map(item -> (Integer) item).count();
            String[] split = key.replace("dies-score:", "").split("-");
            Long serverId = Long.parseLong(split[0]);
            Long playerId = Long.parseLong(split[1]);
            Long steamId = Long.parseLong(split[2]);
            PlayerScoreKill playerScoreKill = playerScoreKillMapper.selectOne(Wrappers.lambdaQuery(PlayerScoreKill.class)
                    .eq(PlayerScoreKill::getServerId, serverId)
                    .eq(PlayerScoreKill::getPlayerId, playerId)
                    .eq(PlayerScoreKill::getCountDate, LocalDate.now()));
            if (playerScoreKill == null) {
                playerScoreKill = new PlayerScoreKill();
                playerScoreKill.setCountDate(LocalDate.now());
                playerScoreKill.setSteamId(steamId);
                playerScoreKill.setServerId(serverId);
                playerScoreKill.setPlayerId(playerId);
                playerScoreKill.setKills(0);
                playerScoreKill.setDies(0);
            }

            playerScoreKill.setDies(playerScoreKill.getDies() + (int) diesCount);

            if (playerScoreKill.getId() == null) {
                playerScoreKillMapper.insert(playerScoreKill);
            } else {
                playerScoreKillMapper.updateById(playerScoreKill);
            }
        }
        log.info("死亡数据统计完成");
    }
}
